package org.se.scut.pinterest.mina.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {

	private static final String  PICTURE_DIRECTORY = "./Images/";
	
	public static String bytesToFile(byte[] bytes, String fileName){
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(new FileOutputStream(PICTURE_DIRECTORY + fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "";
		}
		try {
			out.write(bytes);
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}finally{
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return PICTURE_DIRECTORY + fileName;
	}
	
	public static final byte[] fileToBytes(String fileName, byte[] content){
		BufferedInputStream in;
		try {
			in = new BufferedInputStream(new FileInputStream(fileName));
			in.read(content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}
	
}
